//
//  SignatureData.m
//  Today
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "SignatureData.h"
static sqlite3 *dbsignature = nil;
@implementation SignatureData
-(instancetype)init
{
    self = [super init];
    if ( self) {
        [self createMyFMdatabas];
    }
    return self;
}

//打开数据库
-(void)createMyFMdatabas
{
    NSLog(@"%@", NSHomeDirectory());
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [array lastObject];
    NSString *dbpath = [document stringByAppendingFormat:@"Signature.sqlite"];
    db = [FMDatabase databaseWithPath:dbpath];
}
//创建一句话签名内容收藏夹
//一句话签名的内容插入收藏夹
- (void)createSignatureTable
{
    if ([db open]) {
        NSString *sqlcreate = [NSString stringWithFormat:@"create table if not exists signature(id integer primary key autoincrement, title text, pic text)"];
        BOOL result = [db executeUpdate:sqlcreate];
        if (result) {
            NSLog(@"建表成功");
        }else
        {
            NSLog(@"建表失败");
        }
        [db close];
    }
}
-(BOOL)insertSignatureTable:(SignatureModel *)model
{
    
    if ([db open]) {
        NSString *sqlInsert = [NSString stringWithFormat:@"insert into signature (title, pic) values ('%@', '%@')", model.title, model.pic];
        BOOL result = [db executeUpdate:sqlInsert];
        if (result) {
            SIAlertView *alt = [[SIAlertView alloc]initWithTitle:nil andMessage:@"收藏成功"];
            [alt addButtonWithTitle:@"确定" type:SIAlertViewButtonTypeDefault handler:^(SIAlertView *alertView) {
                
            }];
            [alt show];
            [db close];
            return YES;
        }
        else{
            SIAlertView *alt = [[SIAlertView alloc]initWithTitle:nil andMessage:@"收藏失败"];
            [alt addButtonWithTitle:@"确定" type:SIAlertViewButtonTypeDefault handler:^(SIAlertView *alertView) {
                
            }];
            [alt show];
            [db close];
            return NO;
        }
        
    }
    return YES;
}
//检测是否存在
-(BOOL)isExists:(NSString *)signaturetitle
{
    if ([db open]) {
        NSString *sqlselect = [NSString stringWithFormat:@"select * from signature"];
        FMResultSet *set = [db executeQuery:sqlselect];
        while ([set next]) {
            NSString *title = [set stringForColumn:@"title"];
            if ([signaturetitle isEqualToString:title]) {
                
                [db close];
                return NO;
            }
        }
    }
    
    return YES;
}
- (NSArray *)selectSignaturetable
{
    
    NSMutableArray *array = [NSMutableArray array];
    if ([db open]) {
        NSString *sqlselect = [NSString stringWithFormat:@"select * from signature"];
        FMResultSet *set = [db executeQuery:sqlselect];
        while ([set next]) {
            NSString *title = [set stringForColumn:@"title"];
            [array addObject:title];
        }
    }
    
    return array;
}
- (NSArray *)selectSignaturePic
{
    NSMutableArray *array1 = [NSMutableArray array];
    if ([db open]) {
        NSString *sqlselect = [NSString stringWithFormat:@"select * from signature"];
        FMResultSet *set = [db executeQuery:sqlselect];
        while ([set next]) {
            
            NSString *pic = [set stringForColumn:@"pic"];
            [array1 addObject:pic];
            
        }
    }
    
    return array1;
}
- (void)deleteTable:(NSString *)str
{
    if ([db open]) {
        
        NSString *sqldelete = [NSString stringWithFormat:@"delete from signature where title = '%@'", str];
        BOOL result = [db executeUpdate:sqldelete];
        if (result) {
            NSLog(@"删除成功");
        }else
        {
            NSLog(@"删除失败 %d", result);
        }
    }
    [db close];
}


+ (void)openDataBase{
    if (dbsignature) {
        return;
    }
    NSLog(@"%@", NSHomeDirectory());
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"signatures.rdb"];
    sqlite3_open(path.UTF8String, &dbsignature);
}
+ (void)createTable{
    [self openDataBase];
    NSString *sql = @"create table signature (title varchar(20), pic varchar(20))";
    sqlite3_exec(dbsignature, sql.UTF8String, NULL, NULL, NULL);
}
+ (NSArray *)selectSignaturetable{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbsignature, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            NSString *string = [NSString stringWithUTF8String:(const char *) title];
            [array addObject:string];
        }
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}
+ (NSArray *)selectSignaturePic{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbsignature, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *pic = sqlite3_column_text(stmt, 1);
            NSString *string = [NSString stringWithUTF8String:(const char *) pic];
            [array addObject:string];
        }
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}
+ (BOOL)insertSignatureTable:(SignatureModel *)model{
    [self openDataBase];
    int result = [SignatureData isExists:model.title];
    if (result == 0) {
        NSString *sql = [NSString stringWithFormat:@"insert into signature values ('%@', '%@')", model.title, model.pic];
        sqlite3_exec(dbsignature, sql.UTF8String, NULL, NULL, NULL);
        return YES;
    } else{
        return NO;
    }

}
+ (BOOL)isExists:(NSString *)signaturetitle{
    [self openDataBase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"select * from constellation";
    int result = sqlite3_prepare_v2(dbsignature, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            NSString *string = [NSString stringWithUTF8String:(const char *) title];
            if ([string isEqualToString:signaturetitle]) {
                return YES;
            }
        }
        sqlite3_finalize(stmt);
        return YES;
    }
    sqlite3_finalize(stmt);
    return NO;

}
+ (void)deleteTable:(NSString *)str{
    [self openDataBase];
    NSString *sql = [NSString stringWithFormat:@"delete from signature where title = '%@'", str];
    sqlite3_exec(dbsignature, sql.UTF8String, NULL, NULL, NULL);

}
@end
